
package com.x52im.rbchat.logic.chat_root.face;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.eva.android.ToolKits;
import com.x52im.rbchat.R;
import com.x52im.rbchat.logic.chat_root.model.FaceEntity;
import com.x52im.rbchat.utils.SoftKeyboardStateHelper;

/**
 * 表情面板视图可重用组件实现类。
 *
 * @author FreeMan
 * @since 7.2
 */
public class FaceBoardView extends LinearLayout implements SoftKeyboardStateHelper.SoftKeyboardStateListener {
    public static final int LAYOUT_TYPE_HIDE = 0;
    public static final int LAYOUT_TYPE_FACE = 1;

    private int layoutType = LAYOUT_TYPE_HIDE;

    // 当表情显示后要做的事，对外提供一个接口，供外部继承重写onShowFace()方法
    public interface OnToolBoxListener {
        void onShowFace();
        void onHideFace();
    }

    private LinearLayout mRlFace;
    private EmojiPageFragment emojiPageFragment;

    private Context context;
    private OnToolBoxListener mFaceListener;

    private EditText inputET;

    public FaceBoardView(Context context) {
        this(context, null);
    }

    public FaceBoardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public FaceBoardView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public void setInputEt(EditText et){
        this.inputET = et;
    }

    private void init(Context context) {
        this.context = context;
        View root = View.inflate(context, R.layout.chatting_face_mainboard_view, null);
        this.addView(root);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        addKeyboardStateListener();
        this.initWidget();
    }

    private void addKeyboardStateListener() {
        SoftKeyboardStateHelper mKeyboardHelper = new SoftKeyboardStateHelper(((Activity)getContext())
                .getWindow().getDecorView());
        mKeyboardHelper.addSoftKeyboardStateListener(this);
    }

    @Override
    public void onSoftKeyboardOpened(int keyboardHeightInPx) {
        hideLayout();
    }

    @Override
    public void onSoftKeyboardClosed() {
    }

    private void initWidget() {
        mRlFace = (LinearLayout) findViewById(R.id.toolbox_layout_face);
        FragmentManager fm = ((FragmentActivity)context).getSupportFragmentManager(); //获取Fragment管理器对象
        FragmentTransaction ft = fm.beginTransaction(); //开启事务
        emojiPageFragment =  new EmojiPageFragment();
        ft.add(R.id.toolbox_layout_face, emojiPageFragment); //将fragment添加到布局当中
        ft.commit(); //千万别忘记提交事务
    }

    /*************************
     * 内部方法 start
     ************************/

    private void changeLayout(int mode) {
        layoutType = mode;
    }

    /***************************** 内部方法 end ******************************/

    /**************************
     * 可选调用的方法 start
     **************************/

    public OnClickListener getFunctionBtnListener(final int which) {
        return v -> {
            if (isShow() && which == layoutType) {
                hideLayout();
                ToolKits.showInputMethod((Activity)context, inputET);
            } else {
                ToolKits.hideInputMethod((Activity)context, inputET);
                changeLayout(which);
                showLayout();
                inputET.requestFocus();// 获取焦点，否则再次点击时无法切换到键盘模式
            }
        };
    }

    public void showLayout() {
        ToolKits.hideInputMethod((Activity)context, inputET);
        if(layoutType == LAYOUT_TYPE_FACE)
            mFaceListener.onShowFace();

        // 延迟一会，让键盘先隐藏再显示表情键盘，否则会有一瞬间表情键盘和软键盘同时显示
        mRlFace.postDelayed(() -> {
            mRlFace.setVisibility(VISIBLE);
            if (mFaceListener != null) {
                mFaceListener.onShowFace();
            }
        }, 50);
    }


    public boolean isShow() {
        return mRlFace.getVisibility() == VISIBLE;
    }

    public void hideLayout() {
        if(isShow()){
            mRlFace.setVisibility(GONE);
            if(layoutType == LAYOUT_TYPE_FACE)
                mFaceListener.onHideFace();
        }
    }

    public void setOnOperationListener(FaceOnOperationListener onOperationListener) {
        emojiPageFragment.setOnOperationListener(onOperationListener);
    }

    public void setOnToolBoxListener(OnToolBoxListener mFaceListener) {
        this.mFaceListener = mFaceListener;
    }

    /**
     * 表情面板监听器接口。
     *
     * @author Freeman
     * @since 7.2
     */
    public interface FaceOnOperationListener {
        void selectedIEmoji(FaceEntity iEmoji);
        void selectedBackSpaceInIEmoji();
    }
}


